﻿using Publicuse.Entity.other;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Publicuse.Util
{
    /// <summary>
    /// 计算地图上某个坐标点的是否在多边形内
    /// </summary>
    public class GpsPolygonUtil
    {
        /// <summary>
        /// 坐标点是否在多边形内判断
        /// </summary>
        /// <param name="point"></param>
        /// <param name="pts"></param>
        /// <returns></returns>
        public static bool isPointInPolygon(Location point, List<Location> pts)
        {
            //检查类型
            if (point == null || pts == null)
                return false;

            var N = pts.Count;
            var boundOrVertex = true; //如果点位于多边形的顶点或边上，也算做点在多边形内，直接返回true
            var intersectCount = 0; //交叉点计数x 
            var precision = 2e-10; //浮点类型计算时候与0比较时候的容差
            Location p1, p2;
            var p = point;
            p1 = pts[0];
            for (var i = 1; i <= N; ++i)
            {
                if (p.lat.Equals(p1.lat) && p.lng.Equals(p1.lng))
                {
                    return boundOrVertex;
                }
                p2 = pts[i % N];
                if (p.lat < Math.Min(p1.lat, p2.lat) || p.lat > Math.Max(p1.lat, p2.lat))
                {
                    p1 = p2;
                    continue;
                }
                if (p.lat > Math.Min(p1.lat, p2.lat) && p.lat < Math.Max(p1.lat, p2.lat))
                {
                    if (p.lng <= Math.Max(p1.lng, p2.lng))
                    {
                        if (p1.lat == p2.lat && p.lng >= Math.Min(p1.lng, p2.lng))
                        {
                            return boundOrVertex;
                        }

                        if (p1.lng == p2.lng)
                        {
                            if (p1.lng == p.lng)
                            {
                                return boundOrVertex;
                            }
                            else
                            {
                                ++intersectCount;
                            }
                        }
                        else
                        {
                            var xinters =
                                (p.lat - p1.lat) * (p2.lng - p1.lng) / (p2.lat - p1.lat) +
                                p1.lng;
                            if (Math.Abs(p.lng - xinters) < precision)
                            {
                                return boundOrVertex;
                            }
                            if (p.lng < xinters)
                            {
                                ++intersectCount;
                            }
                        }
                    }
                }
                else
                {
                    if (p.lat == p2.lat && p.lng <= p2.lng)
                    {
                        var p3 = pts[(i + 1) % N];
                        if (p.lat >= Math.Min(p1.lat, p3.lat) && p.lat <= Math.Max(p1.lat, p3.lat))
                        {
                            ++intersectCount;
                        }
                        else
                        {
                            intersectCount += 2;
                        }
                    }
                }
                p1 = p2;
            }
            if (intersectCount % 2 == 0)
            {
                //偶数在多边形外
                return false;
            }
            else
            {
                //奇数在多边形内
                return true;
            }
        }

        /// <summary>
        /// 判断点是否在多边形内
        /// </summary>
        /// <param name="checkPoint">需要判断的点</param>
        /// <param name="polygonPoints">组成多边形点的集合</param>
        /// <returns></returns>
        public static bool IsInPolygon2(Location checkPoint, List<Location> polygonPoints)
        {
            int counter = 0;
            int i;
            double xinters;
            Location p1, p2;
            int pointCount = polygonPoints.Count;
            p1 = polygonPoints[0];
            for (i = 1; i <= pointCount; i++)
            {
                p2 = polygonPoints[i % pointCount];
                if (checkPoint.lng > Math.Min(p1.lng, p2.lng)//校验点的Y大于线段端点的最小Y
                    && checkPoint.lng <= Math.Max(p1.lng, p2.lng))//校验点的Y小于线段端点的最大Y
                {
                    if (checkPoint.lat <= Math.Max(p1.lat, p2.lat))//校验点的X小于等线段端点的最大X(使用校验点的左射线判断).
                    {
                        if (p1.lng != p2.lng)//线段不平行于X轴
                        {
                            xinters = (checkPoint.lng - p1.lng) * (p2.lat - p1.lat) / (p2.lng - p1.lng) + p1.lat;
                            if (p1.lat == p2.lat || checkPoint.lat <= xinters)
                            {
                                counter++;
                            }
                        }
                    }

                }
                p1 = p2;
            }

            if (counter % 2 == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
